package com.tencent.wxcloudrun.frontcontroller;

import com.tencent.wxcloudrun.dto.*;
import com.tencent.wxcloudrun.mapper.SkulistMapper;
import com.tencent.wxcloudrun.service.ShippingCarService;
import com.tencent.wxcloudrun.service.ShippingcaradditionsService;
import com.tencent.wxcloudrun.service.ShippingcarskuService;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author lichongbing
 * @version 1.0.0
 * @createdate 2023/3/4 19:09
 * @description: TODO
 */
@RestController
@Api(tags = "购物车模块")
public class ShippingCarController {

    @Autowired
    private ShippingCarService shippingCarService;

    @Autowired
    private ShippingcarskuService shippingcarskuService;

    @Autowired
    private ShippingcaradditionsService shippingcaradditionsService;

    @Autowired
    private SkulistMapper skulistMapper;

    @Operation(summary = "读取购物车数据免code")
    @GetMapping(value = "/wxcloud/shippingCarInfo")
    ShippingCarInfoResponse shippingCarInfo(HttpServletRequest request) {
        String openid = request.getHeader("X-WX-OPENID");
        //获取购物车商品信息
        List<Shippingcar> shippingcars = shippingcaradditionsService.getShippingcar(openid);
        Integer price = 0;
        Integer number = 0;
        for (int i = 0; i < shippingcars.size(); i++) {
            number += shippingcars.get(i).getNumber();
            //获取购物车商品sku信息
            StringBuffer stringBuffer = new StringBuffer();
            List<Shippingcarsku> shippingcarskus = shippingcarskuService.getShippingcarsku(shippingcars.get(i).getGoodid());
            for (int j = 0; j < shippingcarskus.size(); j++) {
                Shippingcarsku shippingcarsku = shippingcarskus.get(j);
                String optionname = shippingcarsku.getOptionname();
                stringBuffer.append(optionname);
                stringBuffer.append(":");
                String optionvalue = shippingcarsku.getOptionvaluename();
                stringBuffer.append(optionvalue);
                stringBuffer.append(",");
            }
            String propertychildNames = stringBuffer.toString();
            //获取购物车商品价格
            if (propertychildNames.equals("")) {
                Map<String, Integer> goodsPrices = skulistMapper.getGoodsPrices(shippingcars.get(i).getGoodid());
                shippingcars.get(i).setPrice(goodsPrices.get("originalPrice"));
                price += goodsPrices.get("originalPrice") * shippingcars.get(i).getNumber();
            } else {
                GoodsPrice prices = skulistMapper.getGoodsPrice2(shippingcars.get(i).getGoodid(), propertychildNames);
                shippingcars.get(i).setPrice(prices.getPrice());
                price += prices.getPrice() * shippingcars.get(i).getNumber();
            }


            shippingcars.get(i).setSku(shippingcarskus);
            //获取购物车商品附加信息
            List<Shippingcaradditions> shippingcaradditions = shippingcaradditionsService.getShippingcaradditions(shippingcars.get(i).getGoodid());
            shippingcars.get(i).setAdditions(shippingcaradditions);
        }
        ShippingCarInfoResponse shippingCarInfoResponse = new ShippingCarInfoResponse();
        ShippingCarInfo shippingCarInfo = new ShippingCarInfo();
        shippingCarInfo.setItems(shippingcars);
        //获取购物车商品总价
        shippingCarInfo.setPrice(String.valueOf(price));
        //获取购物车商品总数
        shippingCarInfo.setNumber(String.valueOf(number));
        shippingCarInfoResponse.setData(shippingCarInfo);
        return shippingCarInfoResponse;

    }

    /**
     * 加入购物车
     */
    @Operation(summary = "加入购物车")
    @PostMapping("/wxcloud/shippingCarInfoAddItem1")
    ShippingCarInfoAddItemResponse shippingCarInfoAddItem(HttpServletRequest request, @RequestBody ShippingCarInfoAddItemRequest json) {
        String openid = request.getHeader("X-WX-OPENID");
        List<GoodsAddition> goodsAddition = json.getGoodsAddition();
        List<Sku> sku = json.getSku();
        String goodsId = json.getGoodsId();
        Integer number = json.getNumber();
        //判断购物车是否存在该商品
        Shippingcar shippingcars = shippingCarService.getShippingCars(openid, goodsId);
        if (shippingcars != null) {
            Integer number1 = shippingcars.getNumber();
            //存在则修改购买数量
            int i = shippingCarService.updateShippingCar(shippingcars.getId(), number+number1);
            ShippingCarInfoAddItemResponse shippingCarInfoAddItemResponse = new ShippingCarInfoAddItemResponse();
            shippingCarInfoAddItemResponse.setCode(0);
            shippingCarInfoAddItemResponse.setMsg("ok");
            shippingCarInfoAddItemResponse.setData("");
            return shippingCarInfoAddItemResponse;
        }else {
            String shippingcarid = shippingCarService.addShippingCar(openid, goodsId, number);
            shippingcarskuService.addShippingCarSku(sku, shippingcarid);
            shippingcaradditionsService.addShippingCarAdditions(goodsAddition, shippingcarid);
            ShippingCarInfoAddItemResponse shippingCarInfoAddItemResponse = new ShippingCarInfoAddItemResponse();
            shippingCarInfoAddItemResponse.setCode(0);
            shippingCarInfoAddItemResponse.setMsg("ok");
            shippingCarInfoAddItemResponse.setData("");
            return shippingCarInfoAddItemResponse;
        }

    }


    /**
     * 购物车修改购买数量
     */
    @Operation(summary = "购物车修改购买数量")
    @PostMapping("/wxcloud/shippingCarInfoModifyNumber")
    ShippingCarInfoModifyNumberResponse shippingCarInfoModifyNumber(HttpServletRequest request, @RequestBody ShippingCarInfoModifyNumberRequest json) {
        int i = shippingCarService.updateShippingCar(json.getKey(), json.getNumber());
        ShippingCarInfoModifyNumberResponse shippingCarInfoModifyNumberResponse = new ShippingCarInfoModifyNumberResponse();
        shippingCarInfoModifyNumberResponse.setCode(0);
        shippingCarInfoModifyNumberResponse.setMsg("ok");
        return shippingCarInfoModifyNumberResponse;
    }

    /**
     * 移除购物车中某条记录
     */
    @Operation(summary = "移除购物车中某条记录")
    @PostMapping("/wxcloud/shippingCarInfoRemoveItem")
    ShippingCarInfoRemoveItemResponse shippingCarInfoRemoveItem(HttpServletRequest request, @RequestBody ShippingCarInfoRemoveItemRequest json) {
        String openid = request.getHeader("X-WX-OPENID");
        shippingCarService.deleteShippingCar(json.getKey());
        ShippingCarInfoRemoveItemResponse shippingCarInfoRemoveItemResponse = new ShippingCarInfoRemoveItemResponse();
        shippingCarInfoRemoveItemResponse.setCode(0);
        shippingCarInfoRemoveItemResponse.setMsg("ok");
        return shippingCarInfoRemoveItemResponse;
    }


    @Operation(summary = "清空购物车")
    @PostMapping("/wxcloud/shippingCarInfoRemoveAll")
    ShippingCarInfoRemoveAllResponse shippingCarInfoRemoveAll(HttpServletRequest request, @RequestBody ShippingCarInfoRemoveAllRequest json) {
        String openid = request.getHeader("X-WX-OPENID");
        shippingCarService.deleteShippingCarAll(openid);
        return null;
    }

}
